package com.situ.company.controller;

import com.situ.company.entity.Student;
import com.situ.company.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;

@Controller
@RequestMapping("/student")
public class StudentController {
    // 注入过来使用
    @Autowired
    StudentMapper studentMapper;

    // localhost:8080/student/selectAll
    @RequestMapping("/selectALL")
    @ResponseBody // 返回json格式的数据
    public ArrayList<Student> selectALL() {
        System.out.println("StudentController.selectALL");
        ArrayList<Student> list = studentMapper.selectAll();
        return list;
    }

    // localhost:8080/student/selectAll
    @RequestMapping("/selectAll")
    public String selectAll(Model model) {
        System.out.println("StudentController.selectAll");
        ArrayList<Student> list = studentMapper.selectAll();
        // 把查找出来的数据放到内存里面
        model.addAttribute("list", list);
        // 转发到界面展示   "/"代表的就是webapp这个目录
        return "/student_list.jsp";
    }

    // /student/deleteById?id=12
    @RequestMapping("/deleteById")
    public String deleteById(Integer id) {
        System.out.println("StudentController.deleteById id: " + id);
        studentMapper.deleteById(id);

        // 删除完了之后，重定向到: /student/selectAll
        return "redirect:/student/selectAll";
    }

    @RequestMapping("/add")
    public String add(Student student) {
        System.out.println(student);
        studentMapper.add(student);

        // 删除完了之后，重定向到: /student/selectAll
        return "redirect:/student/selectAll";
    }
}
